package cn.itcast.saveqq;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements
        View.OnClickListener {
    private String TAG="MainActivity";
    private EditText etNumber;
    private EditText etPassword;
    private Button btnLogin;

    private Handler handler= new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                String str=msg.obj.toString();
                Log.i(TAG,str);

                SaveUserName("jeecg","123456");
                Intent intent = new Intent(MainActivity.this,HomeActivity.class);
                startActivity(intent);
                finish();
            }
        }
    };

    private void SaveUserName(String number,String password){
        //保存用户信息
        boolean isSaveSuccess = SPSaveQQ.saveUserInfo(MainActivity.this, number, password);
        //        boolean isSaveSuccess = FileSaveQQ.saveUserInfo(this, number, password);
        if (isSaveSuccess) {
            Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化界面
        initView();
        Map<String, String> userInfo = SPSaveQQ.getUserInfo(this);
//        Map<String, String> userInfo = FileSaveQQ.getUserInfo(this);
        if (userInfo != null) {
            etNumber.setText(userInfo.get("number"));
            etPassword.setText(userInfo.get("password"));
        }
    }
    private void initView() {
        etNumber = (EditText) findViewById(R.id.et_number);
        etPassword = (EditText) findViewById(R.id.et_password);
        btnLogin = (Button) findViewById(R.id.btn_login);
        //设置按钮的点击事件
        btnLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //当单击“登录”按钮时，获取QQ账号和密码
        String number = etNumber.getText().toString().trim();
        String password = etPassword.getText().toString();
        //检验账号和密码是否正确
        if (TextUtils.isEmpty(number)) {
            Toast.makeText(this, "请输入QQ账号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        String url="http://111.225.222.181:8080/jeecg-boot/sys/mLogin";
        OkHttpClient client = new OkHttpClient();
        final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

        Gson gson = new Gson();
        Map map = new HashMap<>();
        map.put("username", "jeecg");
        map.put("password", "123456");
        String param= gson.toJson(map);

        RequestBody requestBody = RequestBody.create(JSON, param);
        final Request request = new Request.Builder()
                .post(requestBody)
                .url(url)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: 失败"+e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Message msg= new Message();
                msg.what=1;
                msg.obj=result;
                handler.sendMessage(msg);
                Log.d(TAG, "onResponse: " + result);
            }
        });
        //登录成功
    }
}
